<?php
declare(strict_types=1);

namespace app\utils;

/***
 * 接口统一响应工具类
 *
 * @package App\Utils
 */
class AppRespUtil
{
    const SUCCESS = 1000;   //成功
    const FAIL = 1001;   //失败
    const NOT_LOGIN = 1002;   //未登录或登录过期

    /**
     * 成功调用
     *
     * @param array $data
     * @param string $msg
     * @return string
     */
    public static function success($data = [], string $msg = 'success')
    {
        $data = empty($data) ? new \stdClass() : $data;
        return json()->data([
            'code' => self::SUCCESS,
            'msg' => $msg,
            'data' => $data
        ]);
    }

    /**
     * 失败调用
     *
     * @param string $msg
     * @param int $code
     * @param array $data
     * @return string
     */
    public static function error(string $msg = 'fail', int $code = self::FAIL, array $data = [])
    {
        $data = empty($data) ? new \stdClass() : $data;
        return json()->data([
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ]);
    }

    /**
     * 需要登陆的错误
     *
     * @param array $data
     * @return string
     */
    public static function login(array $data = [])
    {
        $data = empty($data) ? new \stdClass() : $data;
        return json()->data([
            'code' => self::NOT_LOGIN,
            'msg' => '登陆过期',
            'data' => $data
        ]);
    }

    public static function codeBase(int $code=1000,string $msg='操作成功',array $data=[]){
        $data = empty($data) ? new \stdClass() : $data;
        return json()->data([
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ]);
    }
}
